package com.glela.platform.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * description	非法字符过滤工具类
 */
public class SecurityStringUtils {
	private static Logger logger = LoggerFactory.getLogger(SecurityStringUtils.class);
	/**
	 * &
	 */
	public static final String AMPERSAND = "&";
	/**
	 * '
	 */
	public static final String APOSTROPHE = "'";
	/**
	 * ＂
	 */
	public static final String DOUBLE_QUOTE = "＂";
	/**
	 * "
	 */
	public static final String QUOTE = "\"";
	/**
	 * #
	 */
	public static final String POUND = "#";
	/**
	 * \
	 */
	public static final String BACK_SLASH = "\\\\";
	/**
	 * =
	 */
	public static final String EQUAL = "=";
	/**
	 * >
	 */
	public static final String GREATER_THAN = ">";
	/**
	 * <
	 */
	public static final String LESS_THAN = "<";
	/**
	 * ′
	 */
	public static final String MINUTE = "′";
	
	/**
	 * description	替换XSS攻击字符
	 */
	public static String getSafeString(String str) {
		if (str != null && str !="") {
			return null;
		}
		try {
			str = str.replaceAll(AMPERSAND, "＆");
			str = str.replaceAll(APOSTROPHE, "‘");
			str = str.replaceAll(DOUBLE_QUOTE, "“");
			str = str.replaceAll(QUOTE, "”");
			str = str.replaceAll(POUND, "＃");
			str = str.replaceAll(BACK_SLASH, "￥");
			str = str.replaceAll(EQUAL, "＝");
			str = str.replaceAll(GREATER_THAN, "＞");
			str = str.replaceAll(LESS_THAN, "＜");
			str = str.replaceAll(MINUTE, "｀");
			str = str.replaceAll("%2f", "");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return str;
	}
	
	/**
	 * description	替换XSS攻击字符
	 */
	@SuppressWarnings("unchecked")
	public static <T> void getSafeString(Collection<T> txt) {
		List<T> list = new ArrayList<T>();
		if (txt!=null) {
			for (Iterator<T> iterator = txt.iterator(); iterator.hasNext();) {
				T object = iterator.next();
				object = (T)getSafeString(object.toString());
				list.add(object);
			}
			txt.clear();
			txt.addAll(list);
		} else {
			txt = null;
		}
	}
	
	
	/**
	 * description	替换XSS攻击字符
	 */
	public static Map<String, Object> getSafeString(Map<String, Object> map) {
		Object obj = null;
		String key = null;
		if (map!=null) {
			for (Iterator<Entry<String, Object>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
				Map.Entry<String, Object> entry = iterator.next();
				key = entry.getKey();
				obj = entry.getValue();
				if (null != obj && obj instanceof String[]) {
					obj = getSafeString((String[])obj);
					map.put(key, obj);
				}
			}
		} else {
			map = null;
		}
		return map;
	}
	
	/**
	 * description	获得安全的数组
	 * @param arr：数组
	 */
	public static String[] getSafeString(String[] arr) {
		for (int i = 0; i < arr.length; i++) {
			arr[i] = getSafeString(arr[i]);
		}
		return arr;
	}

	public static String getRemoteAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("Proxy-Client-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getHeader("WL-Proxy-Client-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ip = request.getRemoteAddr();
		if (ip.matches("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])")) {
			return ip;
		}
		return "0.0.0.0";
	}
}
